package com.buildings.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.buildings.R;
import com.buildings.api.Api;
import com.buildings.base.BaseActivity;
import com.buildings.bean.LoginBean;
import com.buildings.bean.UserInfoBean;
import com.buildings.http.BaseCallBack;
import com.buildings.http.MyHttpClient;
import com.buildings.util.CustomToast;
import com.buildings.util.DialogUtils;
import com.buildings.util.GPSUtil;
import com.buildings.util.SPUtil;
import com.buildings.util.Utils;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.SpatialReference;
import com.githang.statusbar.StatusBarCompat;

import java.io.IOException;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


public class MainTabActivity extends BaseActivity implements View.OnClickListener {

    @BindView(R.id.btn_task_list)
    Button btnTask;
    @BindView(R.id.btn_user)
    Button btnUser;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_name_top)
    TextView tvNameTop;

    private Dialog myDialog;

    private LogoutReceiver logoutReceiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintab);
        ButterKnife.bind(this);
        myDialog = DialogUtils.showRefreshDialog(activity);
        String userId = (String) SPUtil.get("userId", "");
        getUserInfo(userId);
        logoutReceiver = new LogoutReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("action.logout");
        registerReceiver(logoutReceiver, intentFilter);
    }


    @OnClick({R.id.btn_user, R.id.btn_task_list, R.id.iv_head})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_user:
                startActivity(new Intent(activity, UserActivity.class));
                break;
            case R.id.btn_task_list:
                startActivity(new Intent(activity, ProjectListActivity.class));
                break;
            case R.id.iv_head:

                break;
        }
    }

    private void getUserInfo(String userId) {
        MyHttpClient
                .newBuilder()
                .get()
                .url(Api.USER_INFO + "?userId=" + userId)
//                .addParam(jsonBuilder.toJsonString())
                .build()
                .enqueue(new BaseCallBack<UserInfoBean>() {
                    @Override
                    public void onSuccess(UserInfoBean bean) {
                        super.onSuccess(bean);
                        //{"code":200,"data":{"userId":"ca3ca0d0d557424d94ec8009b6ca6d08","userCode":"18434163012","userName":"sjadmin","sex":"1","sort":null,"birthday":"","officePhone":"","mobilePhone":"18434163012","fax":"","email":"","zwId":"","directLeader":"","extend1":"","extend2":"","extend3":"","extend4":"","extend5":"","extend6":"","extend7":"","extend8":"","extend9":"","extend10":"","isValid":"1","createUser":"c8f1ba6c7cf842409aba43206e9f7442","createTime":"2020-05-09T07:16:01.000+0000","updateUser":"c8f1ba6c7cf842409aba43206e9f7442","updateTime":"2020-05-15T07:06:47.000+0000","idCard":null,"theme":null},"message":null}
                        if (bean != null && bean.getCode() == 200) {
                            if (bean.getData() != null) {
                                if (!TextUtils.isEmpty(bean.getData().getUserName())) {
                                    SPUtil.save("userName", bean.getData().getUserName());
                                    tvName.setText(bean.getData().getUserName());
                                    String name = bean.getData().getUserName();
                                    char first = name.charAt(0);
                                    tvNameTop.setText(first + "");
                                }
                            }
                        }
                        if (myDialog != null)
                            myDialog.dismiss();
                    }

                    @Override
                    public void onFailure(Call call, IOException e) {
                        super.onFailure(call, e);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }

                    @Override
                    public void onError(int code) {
                        super.onError(code);
                        if (myDialog != null)
                            myDialog.dismiss();
                        CustomToast.showToast(activity, "网络异常");
                    }
                });
    }

    class LogoutReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Utils.logout(activity);
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (logoutReceiver != null) {
            unregisterReceiver(logoutReceiver);
        }
    }
}
